//
//  WLFileSocketManager.m
//  WanLian
//
//  Created by 123456 on 2018/12/24.
//  Copyright © 2018 wanlian. All rights reserved.
//

#import "WLFileSocketManager.h"
#import "WLSocketWorker.h"
#import "WLRecallModel.h"
#import "WLUploadWorker.h"
#import "WLDownloadWorker.h"
#import "WLFileTransportHeadModel.h"



@interface WLFileSocketManager ()

@property (nonatomic, strong) WLSocketWorker *fielInfoWorker;
@property (nonatomic, strong) NSMutableArray *uploadWorkerArr;
@property (nonatomic, strong) NSMutableArray *downloadWorkerArr;
@property (nonatomic, strong) NSMutableArray *workersArr;
@property (nonatomic, strong) WLRecallModel *recallModel;
@end

@implementation WLFileSocketManager
delay_property_impl(NSMutableArray, downloadWorkerArr)
delay_property_impl(NSMutableArray, uploadWorkerArr)
impl_single_instance(WLFileSocketManager)
- (void)configure {
    
}
- (WLSocketWorker *)uploadTaskWithModel:(WLFileRequest *)model
                               progress:(void (^)(float))uploadProgress
                                success:(void (^)(NSDictionary *successDict))success
                                failure:(void (^)(NSString *errorMsg, NSInteger errorTag))failure
                                  error:(void (^)(NSError *error))err {
    WLUploadWorker *worker = [[WLUploadWorker alloc] init];
    [worker configWorkerWithFIFOModel:model progress:uploadProgress success:^(NSDictionary * _Nonnull successDict) {
        [worker cancel];
        if (success) {
            success(successDict);
        }
    } failure:^(NSString * _Nonnull errorMsg, NSInteger errorTag) {
        [worker suspend];
        failure(errorMsg, errorTag);
    } error:err];
    [self.workersArr addObject:worker];
    [self.uploadWorkerArr addObject:worker];
    WLWkSelf
    __weak typeof(worker) weakWorker = worker;
    worker.fileTransportCompleteBlock = ^{
        [weakSelf.workersArr removeObject:weakWorker];
        [weakSelf.uploadWorkerArr removeObject:weakWorker];
    };
    [worker resume];
//    dispatch_queue_t queue = dispatch_queue_create([@"sdfds" UTF8String], 0);
    

    return worker;
}

- (WLSocketWorker *)downloadTaskWithModel:(WLFileRequest *)model
                               progress:(void (^)(float process, long expected, long loaded))downloadProgress
                                success:(void (^)(NSDictionary *successDict))success
                                failure:(void (^)(NSString *errorMsg, NSInteger errorTag))failure
                                  error:(void (^)(NSError *error))err {
    WLDownloadWorker *worker = [[WLDownloadWorker alloc] init];
    [worker configDownloadWorkerWithFIFOModel:model progress:downloadProgress success:^(NSDictionary * _Nonnull successDict) {
        [worker cancel];
        if (success) {
            success(successDict);
        }
    } failure:^(NSString * _Nonnull errorMsg, NSInteger errorTag) {
        [worker suspend];
        if (failure) {
            failure(errorMsg, errorTag);
        }
    }  error:err];
    [self.workersArr addObject:worker];
    [self.downloadWorkerArr addObject:worker];
    WLWkSelf
    __weak typeof(worker) weakWorker = worker;
    worker.fileTransportCompleteBlock = ^{
        [weakSelf.workersArr removeObject:weakWorker];
        [weakSelf.downloadWorkerArr removeObject:weakWorker];
    };
    [worker resume];
    //    dispatch_queue_t queue = dispatch_queue_create([@"sdfds" UTF8String], 0);
    
    
    return worker;
}


- (void)blockDataSendComplete:(WLFileUpDownLoadModel *)fileModel {
    if (self.recallModel.successBlock) {
        self.recallModel.successBlock([NSDictionary dictionary]);
    }
}
- (void)blockDataSended:(WLFileRequest *)fileModel {
    long totalSendedLen = 0;
    for (WLFileTransportHeadModel *blockModel in fileModel.blockModelArr) {
        totalSendedLen += blockModel.currentSendIndex * blockModel.sendSizeUnit;
    }
    long totalBytes = fileModel.fileTotalBytes;
    float process = (float)(totalSendedLen * 1.0/ totalBytes);
    if (self.recallModel.progressBlcok) {
        self.recallModel.progressBlcok(process);
    }
    if (totalSendedLen == fileModel.fileTotalBytes) {
        if (self.recallModel.successBlock) {
            self.recallModel.successBlock([NSDictionary dictionary]);
        }
    }
}

@end
